package cn.gov.pbc.dream.pojo.bo;

import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.IncreaseMethod;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.IncreaseCreditType;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.CustomerType;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.CurrencyType;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.BusinessModel;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.FiduciaryDuty;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.ManageMethod;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.RunMethod;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.CollectionMethod;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.Foreclosures;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.Category;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.IncreaseCredit;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.ProductType;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.Grading;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.IncomeProtection;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.CooperationModal;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.IncomeRightTransfer;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.EarlyTerminationRight;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.SponsorSign;
import cn.gov.pbc.dream.common.BankFinancingProductInfoConst.PrincipalProtection;

import cn.gov.pbc.dream.common.BankFinancingProductInfoConst;
import cn.gov.pbc.dream.util.DateTimeUtil;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@ToString
public class PIB1Bo {
    private List<BufferedReader> bufferedReaderList = new ArrayList<>();

    private List<ProductInfo> productInfoList = new ArrayList<>();

    private List<ProductCurrency> productCurrencyList = new ArrayList<>();

    private List<ProductCustomer> productCustomerList = new ArrayList<>();

    private List<IncreaseCredit> increaseCreditList = new ArrayList<>();

    private List<IncreaseMethod> increaseMethodList = new ArrayList<>();

    private static final String INFO_SIGN = "1";

    private static final String CURRENCY_SIGN = "2";

    private static final String CUSTOMER_SIGN = "3";

    private static final String INCREASE_CREDIT_SIGN = "4";

    private static final String INCREASE_METHOD_SIGN = "5";


    @Data
    @ToString
    public class ProductInfo {
        private String code;

        private String bankFinancingOrgCode;

        private BankFinancingProductInfoConst.Category category;

        private String name;

        private String brand;

        private Integer period;

        private String internalCode;

        private BankFinancingProductInfoConst.CollectionMethod collectionMethod;

        private BankFinancingProductInfoConst.ManageMethod manageMethod;

        private BankFinancingProductInfoConst.RunMethod runMethod;

        private BankFinancingProductInfoConst.ProductType productType;

        private BankFinancingProductInfoConst.BusinessModel businessModel;

        private BankFinancingProductInfoConst.IncomeProtection incomeProtection;

        private BankFinancingProductInfoConst.PrincipalProtection principalProtection;

        private BigDecimal highestRateOfReturn;

        private BigDecimal lowestRateOfReturn;

        private Date raiseStartDate;

        private Date raiseEndDate;

        private BankFinancingProductInfoConst.EarlyTerminationRight earlyTerminationRight;

        private BankFinancingProductInfoConst.Foreclosures foreclosures;

        private BankFinancingProductInfoConst.IncreaseCredit increaseCredit;

        private String domesticCustodyOrg;

        private String overseasEscrowCountry;

        private String overseasEscrowOrgName;

        private Date productStartDate;

        private Date productEndDate;

        private String assetPoolCode;

        private BankFinancingProductInfoConst.FiduciaryDuty fiduciaryDuty;

        private BankFinancingProductInfoConst.CooperationModal cooperationModal;

        private BankFinancingProductInfoConst.Grading grading;

        private BankFinancingProductInfoConst.IncomeRightTransfer incomeRightTransfer;

        private BankFinancingProductInfoConst.SponsorSign sponsorSign;

    }

    @Data
    @ToString
    public class ProductCurrency {

        private String bankFinancingProductCode;

        private BankFinancingProductInfoConst.CurrencyType currencyType;

        private String code;
    }

    @Data
    @ToString
    public class ProductCustomer {

        private String bankFinancingProductCode;

        private BankFinancingProductInfoConst.CustomerType customerType;
    }

    @Data
    @ToString
    public class IncreaseCredit {

        private String bankFinancingProductCode;

        private BankFinancingProductInfoConst.IncreaseCreditType increaseCreditType;

    }

    @Data
    @ToString
    public class IncreaseMethod {

        private String bankFinancingProductCode;

        private BankFinancingProductInfoConst.IncreaseMethod increaseMethod;

    }

    public void assembleData(String data) {
        String[] values = data.split("\\|");
        String sign = values[0];
        if (sign.equals(PIB1Bo.INFO_SIGN)) {
            //基本信息
            values = data.split("\\|", 33);
            this.productInfoList.add(this.assembleProductInfo(values));
        } else if (sign.equals(PIB1Bo.CURRENCY_SIGN)) {
            //币种信息
            values = data.split("\\|", 4);
            this.productCurrencyList.add(this.assembleProductCurrency(values));
        } else if (sign.equals(PIB1Bo.CUSTOMER_SIGN)) {
            this.productCustomerList.add(this.assembleProductCustomer(values));
            //客户类型
            values = data.split("\\|", 3);
        } else if (sign.equals(PIB1Bo.INCREASE_CREDIT_SIGN)) {
            this.increaseCreditList.add(this.assembleIncreaseCredit(values));
            //增信机构类型
            values = data.split("\\|", 3);
        } else if (sign.equals(PIB1Bo.INCREASE_METHOD_SIGN)) {
            //增信形式
            values = data.split("\\|", 3);
            this.increaseMethodList.add(this.assembleIncreaseMethod(values));
        }
    }

    private IncreaseMethod assembleIncreaseMethod(String[] values) {
        IncreaseMethod increaseMethod = new IncreaseMethod();
        increaseMethod.setBankFinancingProductCode(values[1]);
        increaseMethod.setIncreaseMethod(
                BankFinancingProductInfoConst.IncreaseMethod.codeOf(Integer.parseInt(values[2]))
        );
        return increaseMethod;
    }

    private IncreaseCredit assembleIncreaseCredit(String[] values) {
        IncreaseCredit increaseCredit = new IncreaseCredit();
        increaseCredit.setBankFinancingProductCode(values[1]);
        increaseCredit.setIncreaseCreditType(
                BankFinancingProductInfoConst.IncreaseCreditType.codeOf(Integer.parseInt(values[2]))
        );


        return increaseCredit;
    }

    private ProductCustomer assembleProductCustomer(String[] values) {
        ProductCustomer productCustomer = new ProductCustomer();
        productCustomer.setBankFinancingProductCode(values[1]);
        productCustomer.setCustomerType(
                BankFinancingProductInfoConst.CustomerType.codeOf(Integer.parseInt(values[2]))
        );
        return productCustomer;
    }

    private ProductCurrency assembleProductCurrency(String[] values) {
        ProductCurrency productCurrency = new ProductCurrency();
        productCurrency.setBankFinancingProductCode(values[1]);
        productCurrency.setCurrencyType(
                BankFinancingProductInfoConst.CurrencyType.codeOf(Integer.parseInt(values[2]))
        );
        productCurrency.setCode(values[3]);


        return productCurrency;
    }

    private ProductInfo assembleProductInfo(String[] values) {
        ProductInfo productInfo = new ProductInfo();

        productInfo.setCode(values[1]);
        productInfo.setBankFinancingOrgCode(values[2]);
        productInfo.setCategory(BankFinancingProductInfoConst.Category.codeOf(Integer.parseInt(values[3])));
        productInfo.setName(values[4]);
        productInfo.setBrand(values[5]);
        if (StringUtils.isNotBlank(values[6])) {
            productInfo.setPeriod(Integer.parseInt(values[6]));
        }
        productInfo.setInternalCode(values[7]);
        productInfo.setCollectionMethod(
                BankFinancingProductInfoConst.CollectionMethod.codeOf(Integer.parseInt(values[8]))
        );
        productInfo.setManageMethod(
                BankFinancingProductInfoConst.ManageMethod.codeOf(Integer.parseInt(values[9]))
        );
        productInfo.setRunMethod(
                BankFinancingProductInfoConst.RunMethod.codeOf(Integer.parseInt(values[10]))
        );
        productInfo.setProductType(
                BankFinancingProductInfoConst.ProductType.codeOf(Integer.parseInt(values[11]))
        );
        productInfo.setBusinessModel(
                BankFinancingProductInfoConst.BusinessModel.codeOf(Integer.parseInt(values[12]))
        );
        productInfo.setIncomeProtection(
                BankFinancingProductInfoConst.IncomeProtection.codeOf(Integer.parseInt(values[13]))
        );
        productInfo.setPrincipalProtection(
                BankFinancingProductInfoConst.PrincipalProtection.codeOf(Integer.parseInt(values[14]))
        );
        if (StringUtils.isNotBlank(values[15])) {
            productInfo.setHighestRateOfReturn(new BigDecimal(values[15]));
        }
        if (StringUtils.isNotBlank(values[16])) {
            productInfo.setLowestRateOfReturn(new BigDecimal(values[16]));
        }
        productInfo.setRaiseStartDate(DateTimeUtil.strToDate(values[17], DateTimeUtil.DASH_DATE_FORMAT));
        productInfo.setRaiseEndDate(DateTimeUtil.strToDate(values[18], DateTimeUtil.DASH_DATE_FORMAT));
        productInfo.setEarlyTerminationRight(
                BankFinancingProductInfoConst.EarlyTerminationRight.codeOf(Integer.parseInt(values[19]))
        );
        productInfo.setForeclosures(
                BankFinancingProductInfoConst.Foreclosures.codeOf(Integer.parseInt(values[20]))
        );
        productInfo.setIncreaseCredit(
                BankFinancingProductInfoConst.IncreaseCredit.codeOf(Integer.parseInt(values[21]))
        );
        productInfo.setDomesticCustodyOrg(values[22]);
        productInfo.setOverseasEscrowCountry(values[23]);
        productInfo.setOverseasEscrowOrgName(values[24]);
        productInfo.setProductStartDate(DateTimeUtil.strToDate(values[25], DateTimeUtil.DASH_DATE_FORMAT));
        productInfo.setProductEndDate(DateTimeUtil.strToDate(values[26], DateTimeUtil.DASH_DATE_FORMAT));
        productInfo.setAssetPoolCode(values[27]);
        productInfo.setFiduciaryDuty(
                BankFinancingProductInfoConst.FiduciaryDuty.codeOf(Integer.parseInt(values[28]))
        );
        productInfo.setCooperationModal(
                BankFinancingProductInfoConst.CooperationModal.codeOf(Integer.parseInt(values[29]))
        );
        if (values.length > 30) {
            productInfo.setGrading(
                    BankFinancingProductInfoConst.Grading.codeOf(Integer.parseInt(values[30]))
            );
            productInfo.setIncomeRightTransfer(
                    BankFinancingProductInfoConst.IncomeRightTransfer.codeOf(Integer.parseInt(values[31]))
            );
            productInfo.setSponsorSign(
                    BankFinancingProductInfoConst.SponsorSign.codeOf(Integer.parseInt(values[32]))
            );
        }
        return productInfo;
    }

}
